<template>
  <div class="permission-management">
    <div class="page-header">
      <h2>权限管理</h2>
      <p>管理系统权限配置和菜单结构</p>
    </div>
    
    <a-card>
      <div class="actions" style="margin-bottom: 16px; text-align: right;">
        <a-button type="primary" @click="handleAdd">
          <PlusOutlined />
          新增权限
        </a-button>
      </div>
      
      <a-table
        :columns="columns"
        :data-source="permissionList"
        :loading="loading"
        row-key="id"
        :pagination="false"
        :default-expand-all-rows="true"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'type'">
            <a-tag :color="getTypeColor(record.type)">
              {{ getTypeText(record.type) }}
            </a-tag>
          </template>
          
          <template v-if="column.key === 'action'">
            <a-space>
              <a-button type="link" size="small" @click="handleEdit(record)">
                编辑
              </a-button>
              <a-popconfirm
                title="确定要删除这个权限吗？"
                @confirm="handleDelete(record)"
              >
                <a-button type="link" size="small" danger>
                  删除
                </a-button>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { PlusOutlined } from '@ant-design/icons-vue'

const loading = ref(false)

const permissionList = ref([
  {
    id: 1,
    name: '仪表盘',
    code: 'dashboard',
    type: 'menu',
    path: '/dashboard',
    sort: 1,
    children: [
      {
        id: 2,
        name: '查看仪表盘',
        code: 'dashboard:view',
        type: 'button',
        path: '',
        sort: 1
      }
    ]
  },
  {
    id: 3,
    name: '系统管理',
    code: 'system',
    type: 'menu',
    path: '/system',
    sort: 2,
    children: [
      {
        id: 4,
        name: '用户管理',
        code: 'system:user',
        type: 'menu',
        path: '/system/user',
        sort: 1,
        children: [
          {
            id: 5,
            name: '查看用户',
            code: 'system:user:view',
            type: 'button',
            sort: 1
          },
          {
            id: 6,
            name: '新增用户',
            code: 'system:user:add',
            type: 'button',
            sort: 2
          },
          {
            id: 7,
            name: '编辑用户',
            code: 'system:user:edit',
            type: 'button',
            sort: 3
          },
          {
            id: 8,
            name: '删除用户',
            code: 'system:user:delete',
            type: 'button',
            sort: 4
          }
        ]
      }
    ]
  }
])

const columns = [
  { title: '权限名称', dataIndex: 'name', key: 'name' },
  { title: '权限编码', dataIndex: 'code', key: 'code' },
  { title: '类型', key: 'type', width: 100 },
  { title: '路径', dataIndex: 'path', key: 'path' },
  { title: '排序', dataIndex: 'sort', key: 'sort', width: 80 },
  { title: '操作', key: 'action', width: 150 }
]

const getTypeColor = (type: string) => {
  const colors = {
    menu: 'blue',
    button: 'green',
    api: 'orange'
  }
  return colors[type as keyof typeof colors] || 'default'
}

const getTypeText = (type: string) => {
  const texts = {
    menu: '菜单',
    button: '按钮',
    api: '接口'
  }
  return texts[type as keyof typeof texts] || type
}

const handleAdd = () => {
  console.log('新增权限')
}

const handleEdit = (record: any) => {
  console.log('编辑权限', record)
}

const handleDelete = (record: any) => {
  console.log('删除权限', record)
}
</script>

<style scoped>
.permission-management {
  padding: 0;
}

.page-header {
  margin-bottom: 24px;
}

.page-header h2 {
  margin: 0 0 8px 0;
  color: #262626;
}

.page-header p {
  margin: 0;
  color: #8c8c8c;
}
</style> 